<template>
  <div>
    <div class="btn">
      <el-button type="warning" plain size="mini" @click="dialogVisible=true">添加</el-button>
    </div>
    <el-dialog
            title="添加种类"
            :visible.sync="dialogVisible"
            width="50%"
            :before-close="before_close_handle">
            <el-form class="form" ref="form" :rules="rule" label-position="right" label-width="60px" :model="addMap">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="addMap.name"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleClose">确 定</el-button>
            </span>
        </el-dialog>
    <el-table
        stripe
        border
        :data="lists"
        style="width: 100%">
        <el-table-column
          label="ID"
          prop="id">
        </el-table-column>
        <el-table-column
          label="名称"
          prop="text">
        </el-table-column>
        <el-table-column
          align="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"  >删除</el-button>
          </template>
        </el-table-column>
    </el-table>
      <div class="page">
        <el-pagination
        layout="prev, pager, next"
        :total="total"
        @prev-click="current_change"
        @next-click="current_change"
        @current-change="current_change">
      </el-pagination>
      </div>
  </div>
</template>
<script>
import { category_add, category_del, category_page } from '@/api/category_api';


export default{
  data(){
    return{
      rule:{
          name:[
            {required:true,message:'请输入种类名称',trigger:'blur'}
          ]
      },
      dialogVisible:false,
      lists:[],
      pageMap:{
        now_page:1,
        limit:10,
      },
      total:0,
      addMap:{
        name:""
      }
    }
  },
  methods:{
    async handleClose(){
      this.$refs['form'].validate(async (valid)=>{
        if(valid){
          const res = await category_add(this.addMap)
          this.$message({
            type:res.data.code==200?'success':'error',
            message:res.data.msg
          })
          this.dialogVisible = false
          this.order_info()
          this.addMap.name=""
        }else{
          return false;
        }
      })
    },
    before_close_handle(){
      this.addMap.name = ""
        this.dialogVisible = false
    },
    current_change(page){
      this.pageMap.now_page = page
      this.order_info()
    },
    handleDelete(_index,row){
       this.$confirm('此操作将永久删除该订单, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const {data:res} = await category_del({id:row.id})
          this.$message({
            type: res.code==200?'success':'error',
            message: res.msg
          });
          if(res.code==200)this.order_info()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
  async order_info(){
    const {data:res} = await category_page(this.pageMap)
    this.lists = res.data.list
    this.total = res.data.total
  },
  },
  created(){
    this.order_info()
  }
}
</script>
<style lang="scss" scoped>
.page{
  float: right;
  margin-top: 2%;
}
.btn{
  margin-bottom: 2%;
}
</style>